Defined in header <fkYAML/node.hpp>
fkyaml::basic_node::operator[]¶
// (1)
template <typename KeyType>
basic_node& operator[](KeyType&& key);
template <typename KeyType>
const basic_node& operator[](KeyType&& key) const;
// (2)
template <typename BasicNodeType>
basic_node& operator[](BasicNodeType&& key);
template <typename BasicNodeType>
const basic_node& operator[](BasicNodeType&& key) const;
Access to an element in a container node with either an index or key value.
This function must be called on a container node, or a fkyaml::type_error
would be thrown.
The input parameter key
must be either a basic_node
object or an object of a compatible type, i.e., a type with which a basic_node
object can be constructible.
Note that the overload (1) internally constructs a temporal basic_node
object.
So, if you use the same key multiple times, for example, in a for loop, consider creating a basic_node
as a key first for better performance.
Accessing an element with an invalid index (Undefined Behavior)
This function does not check the size of the queried sequence before accessing the element.
To avoid undefined behaviors, please make sure the argument key
is smaller than the queried sequence size with the size()
function in advance.
Or use at()
function instead, which executes the bounds check before accessing an element with a given index.
Accessing an element with a non-existent key
This function does not check the existence of the given key
in the queried mapping.
If the key does not exist, a basic_node object will be default-constructed.
If such a behavior is unwanted, choose one of the followings.
* use at()
function instead, which executes additional checks before accessing an element.
* call the contains()
function in advance to make sure that the key exists in the queried mapping.
Template Parameters¶
- KeyType
- A compatible key type.
- BasicNodeType
- A basic_node template instance type.
Parameters¶
key
[in]- A key to a target element in the sequence/mapping node.
Return Value¶
(Constant) reference to the node value which is associated with the given key.
Examples¶
Overload(1): access an element with compatible keys
#include <iostream>
#include <fkYAML/node.hpp>
int main() {
// create a YAML sequence node.
fkyaml::node n1 = {123, 234, 345, 456};
// print YAML nodes at the following indexes.
std::cout << n1[0] << std::endl;
std::cout << n1[1] << std::endl;
std::cout << n1[2] << std::endl;
std::cout << n1[3] << std::endl;
// this will cause an undefined behavior!
// std::cout << n1[4] << std::endl;
// create a YAML mapping node.
fkyaml::node n2 = {{"foo", true}, {"bar", 123}};
// print YAML nodes associated with the following keys.
std::cout << std::boolalpha << n2["foo"] << std::endl;
std::cout << n2["bar"] << std::endl;
// try to access a YAML node with a key which does not exist.
std::cout << n2[true] << std::endl;
return 0;
}
output:
Overload(2): access an element with basic_node
keys
#include <iostream>
#include <fkYAML/node.hpp>
int main() {
// create a YAML sequence node.
fkyaml::node n1 = {123, 234, 345, 456};
// print YAML nodes at the following indexes.
fkyaml::node index_zero = 0;
fkyaml::node index_one = 1;
fkyaml::node index_two = 2;
fkyaml::node index_three = 3;
std::cout << n1[index_zero] << std::endl;
std::cout << n1[index_one] << std::endl;
std::cout << n1[index_two] << std::endl;
std::cout << n1[index_three] << std::endl;
// this will cause an undefined behavior!
// fkyaml::node index_four = 4;
// std::cout << n1[index_four] << std::endl;
// create a YAML node.
fkyaml::node n2 = {{"foo", true}, {"bar", 123}};
// print YAML nodes associated with the following keys.
fkyaml::node foo_key = "foo";
fkyaml::node bar_key = "bar";
std::cout << std::boolalpha << n2[foo_key] << std::endl;
std::cout << n2[bar_key] << std::endl;
// try to access a YAML node with a key which does not exist.
fkyaml::node true_key = true;
std::cout << n2[true_key] << std::endl;
return 0;
}
output: